package com.it.park.controller.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.it.park.client.service.MsgService;
import com.it.park.client.service.UserFriendService;
import com.it.park.client.service.UserMsgService;
import com.it.park.common.JsonObj;
import com.it.park.common.SessionUtil;
import com.it.park.constant.Constant;
import com.it.park.model.Msg;
import com.it.park.model.UserFriend;
import com.it.park.model.UserMsg;
import com.it.park.rms.client.model.User;
import com.it.park.rms.client.service.UserService;

/**
 * @author Administrator
 *我的私信
 */
@Controller
@RequestMapping("/user/notifications")
public class NotificationsController {
	private String rootPath=Constant.USER_PATH;

	@Autowired
	private MsgService msgService;
	@Autowired
	private UserMsgService userMsgService;
	@Autowired
	private UserFriendService userFriendService;
	@Autowired
	private UserService userService;
	
	/**
	 * 消息页面
	 */
	@RequestMapping("notificationsManage")
	public String notificationsManage(HttpServletRequest request,HttpSession session){
		User user = (User) session.getAttribute("user");
		List<Msg> msgs = msgService.selectPrivateMsg(user.getId());
		request.setAttribute("msgs",msgs);
		int total = msgService.getMsgTotal(user.getId());//未读消息数量
		request.setAttribute("total", total);
		return rootPath+"notifications/notificationsManage";
	}
	
	/**
	 * 消息详情
	 */
	@RequestMapping("notificationsDetail")
	public String notificationsDetail(Integer id,HttpServletRequest request,HttpSession session){
		Msg msg = msgService.selectByPrimaryKey(id);
		User user = (User) session.getAttribute("user");
		UserMsg record = new UserMsg();
		record.setStatus(1);
		record.setUserId(user.getId());
		record.setMsgId(id);
		userMsgService.updateUserMsgStatus(record);
		request.setAttribute("msg",msg);
		return rootPath+"notifications/notificationsDetail";
	}
	
	/**
	 * 个人未读消息
	 */
	@RequestMapping("notReadMsgTotal")
	@ResponseBody
	public JsonObj notReadMsgTotal(HttpServletRequest request,HttpSession session){
			User user = (User) session.getAttribute("user");
			int  total = msgService.getMsgTotal(user.getId());
			JsonObj json = new JsonObj();
			json.setObject(total);
			json.setSuccess(true);
			return json;
	}
	
	/**
	 * 私信页面
	 */
	@RequestMapping("personalLetter")
	public String personalLetter(Integer userId,HttpServletRequest request,HttpSession session){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("type", 1);
		map.put("userId", SessionUtil.getUserID(session));
		List<UserFriend> friends =  userFriendService.selectAllFriends(map);
		if(userId != null){
			User u = userService.selectByPrimaryKey(userId);
			request.setAttribute("u",u);
		}
		request.setAttribute("friends",friends);
		return rootPath+"notifications/personalLetter";
	}
	
	/**
	 * @页面
	 */
	@RequestMapping("ats")
	public String ats(HttpServletRequest request,HttpSession session){
		return rootPath+"notifications/ats";
	}
	
	/**
	 * 我的关系（我关注的）
	 */
	@RequestMapping("attention")
	public String attention(HttpServletRequest request,HttpSession session){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("type", 1);
		map.put("userId", SessionUtil.getUserID(session));
		List<UserFriend> userFriends =  userFriendService.selectAllFriends(map);
		request.setAttribute("userFriends", userFriends);
		return rootPath+"notifications/attention";
	}
	
	/**
	 * 关注我的
	 */
	@RequestMapping("attentionMe")
	public String attentionMe(HttpServletRequest request,HttpSession session){
	/*	Map<String,Object> map = new HashMap<String,Object>();
		map.put("type",2);
		map.put("userId", SessionUtil.getUserID(session));
		List<UserFriend> list =  userFriendService.selectAllFriends(map);
		List<User> users = userService.selectByList(list);
		request.setAttribute("users", users);*/
		return rootPath+"notifications/attentionMe";
	}
	
	/**
	 * 相互关注
	 */
	@RequestMapping("attentionEach")
	public String attentionEach(HttpServletRequest request,HttpSession session){
		List<UserFriend> list = new ArrayList<>();
		Map<String,Object> map = new HashMap<String,Object>();
		Integer userId = SessionUtil.getUserID(session);
		map.put("type", 1);
		map.put("userId", userId);
		List<UserFriend> ls =  userFriendService.selectAllFriends(map);
		for (UserFriend uf : ls) {
			UserFriend uFriend = new UserFriend(uf.getUserFriendId(),userId);
			UserFriend uFriend2 = userFriendService.selectHasExists(uFriend);
			if(uFriend2 != null){
				list.add(uFriend2);
			}
		}
//		List<User> users = userService.selectByList(list);
//		request.setAttribute("users", users);
		return rootPath+"notifications/attentionEach";
	}
	
	/**
	 * 取消关注
	 */
	@RequestMapping("cancelAttention")
	public String cancelAttention(Integer userFriendId,HttpServletRequest request,HttpSession session){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userFriendId",userFriendId);
		map.put("userId", SessionUtil.getUserID(session));
		userFriendService.cancelAttention(map);
		return "redirect:attention";
	}
	
	/**
	 * 发送站内消息
	 */
	@RequestMapping("sendMsg")
	@ResponseBody
	public JsonObj sendMsg(Msg msg,HttpServletRequest request,HttpSession session){
		JsonObj json = new JsonObj();
		User user = SessionUtil.getSessionUser(session);
		if(user.getId().intValue() == msg.getUserId().intValue()){//自己不能给自己发私信
			json.setMessage(Constant.FAIL_SEND_PRIVARE_SELF_MSG);
			json.setSuccess(false);
		}else{
			msg.setMsgTitle("来至站内【"+user.getChName()+"】消息!");
			msg.setCreateDate(new Date());
			boolean success = msgService.insertAndGetInsertID(msg);
			if(success){
				json.setMessage(Constant.SUCCESS_MSG);
				UserMsg userMsg = new UserMsg();
				userMsg.setUserId(msg.getUserId());
				userMsg.setMsgId(msg.getId());
				userMsgService.insertSelective(userMsg);
				json.setSuccess(true);
			}else{
				json.setMessage(Constant.FAIL_MSG);
				json.setSuccess(false);
			}
		}
		return json;
	}
	
	
	
	
	
}
